<?php
namespace app\controllers;

use app\common\corelib\CacheHelper;
use app\common\HttpCurl;
use app\logic\OrderLogic;
use app\logic\UserLogic;
use Yii;

//前端用户接口
class UserController extends BaseController
{

    /*public function actionGetOpenid(){
        $appid = Yii::$app->params['wx']['appid'];
        $return_url = urlencode(Yii::$app->params['domain']['all_domain'].'/user/get-openid-next');
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$return_url&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        jump($url);
    }*/
    //获取微信个人信息接口
    //public function actionGetOpenidNext(){
    public function actionGetOpenid(){
        /*header("Location:http://mdl1.082818.com/login.html?open_id=ouUTDjsIKRXDvL6Hk0A3uJFTgnJo");
        exit;*/
        $referrer = $this->getQueryparams('referrer',"");
        //var_dump($referrer);exit;
        $code = $this->getQueryparams('code',"");
        $appid = Yii::$app->params['wx']['appid'];
        $secret = Yii::$app->params['wx']['secret'];
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";
        $data = HttpCurl::http($url,'GET');
        //file_put_contents(date("Ymd")."_testtemp.txt", json_encode($data)." 日期：".date("Y-m-d H:i:s",time()).PHP_EOL,FILE_APPEND);
        //file_put_contents(date("Ymd")."_testtemp1.txt", $referrer." 日期：".date("Y-m-d H:i:s",time()).PHP_EOL,FILE_APPEND);
        //var_dump($data);exit;
        $open_id = $data['openid'];
        $access_token = $data['access_token'];
        $refresh_token = $data['refresh_token'];
        $userLogic = new UserLogic();
        //file_put_contents(date("Ymd")."_testtemp2.txt", '123'." 日期：".date("Y-m-d H:i:s",time()).PHP_EOL,FILE_APPEND);
        $user = $userLogic->getByOpenid($open_id,$access_token,$refresh_token);
        //file_put_contents(date("Ymd")."_testtemp2.txt", '456'." 日期：".date("Y-m-d H:i:s",time()).PHP_EOL,FILE_APPEND);
        //jump($referer.'?open_id='.$open_id);
        if(strpos($referrer,'?') !== false){
            //file_put_contents(date("Ymd")."_testtemp2.txt", $referrer."&open_id=".$open_id." 日期：".date("Y-m-d H:i:s",time()).PHP_EOL,FILE_APPEND);
            header("Location:".$referrer."&open_id=".$open_id);
        }
        else{
            //file_put_contents(date("Ymd")."_testtemp2.txt", $referrer."?open_id=".$open_id." 日期：".date("Y-m-d H:i:s",time()).PHP_EOL,FILE_APPEND);
            header("Location:".$referrer."?open_id=".$open_id);
        }

        exit;
        //$this->response(['open_id'=>$open_id]);
    }
    //添加分享
    public function actionAddShare(){
        $open_id = $this->getQueryparams('open_id',"");
        $userLogic = new UserLogic();
        $userLogic->addShare($open_id);
        $this->response();
    }

    /*public function actionUpdateLoginTime(){
        $open_id = $this->getQueryparams('open_id',"");
        $userLogic = new UserLogic();
        $userLogic->updateLoginTime($open_id);
        $this->response();
    }*/
    //登录接口
    public function actionLogin(){
        $open_id = $this->getQueryparams('open_id',"");
        $username = $this->getQueryparams('username',"");
        $password = $this->getQueryparams('password',"");
        $userLogic = new UserLogic();
        $result = $userLogic->login($open_id,$username,$password);
        if($result){
            $this->response(['token'=>$result]);
        }else{
            $this->error('50000','账号或密码错误');
        }
    }



    /**
     * 个人中心首页
     * @apiDescription   by Jim
     * @api              {post} /user/index 个人中心首页
     * @apiName          index
     * @apiVersion       1.0.0
     * @apiParam {string} token 用户token
     * @apiGroup         User
     * @apiSampleRequest /user/index?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * @apiError         40004    获取失败
     */
    public function actionIndex(){

    }



}
